import {createRouter, createWebHashHistory} from "vue-router";
import store from "../store";
import Home from "../views/Home.vue";

const routes = [
  {
    path: "/",
    redirect: "/dashboard",
    meta: {
      title: "系统首页",
    },
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/dashboard",
        name: "dashboard",
        meta: {
          title: "系统首页",
        },
        component: () =>
          import(/* webpackChunkName: "dashboard" */ "../views/Dashboard.vue"),
      },
      {
        path: "/annoucement",
        name: "annoucement",
        meta: {
          title: "公告",
        },
        component: () =>
          import(
            /* webpackChunkName: "annoucement" */ "../views/Annoucement.vue"
          ),
      },
      {
        path: "/competitionCreate",
        name: "competitionCreate",
        meta: {
          title: "赛事创建",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "competition" */ "../views/CompetitionCreate.vue"
          ),
      },
      {
        path: "/competitionEdit",
        name: "competitionEdit",
        meta: {
          title: "赛事编辑",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "competitionEdit" */ "../views/CompetitionEdit.vue"
          ),
      },
      {
        path: "/schedule",
        name: "schedule",
        meta: {
          title: "赛程",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "scheduleManage" */ "../views/Schedule.vue"
          ),
      },
      {
        path: "/scoreManage",
        name: "scoreManage",
        meta: {
          title: "成绩管理",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "scoreManage" */ "../views/ScoreManage.vue"
          ),
      },
      {
        path: "/enrollManage",
        name: "enrollManage",
        meta: {
          title: "报名处理",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "enrollManage" */ "../views/EnrollManage.vue"
          ),
      },
      {
        path: "/enroll",
        name: "enroll",
        meta: {
          title: "参赛报名",
        },
        component: () =>
          import(/* webpackChunkName: "enroll" */ "../views/Enroll.vue"),
      },
      {
        path: "/algorithm",
        name: "algorithm",
        meta: {
          title: "编排",
          isAuth: true,
        },
        component: () =>
          import(/* webpackChunkName: "algorithm" */ "../views/Algorithm.vue"),
      },
      {
        path: "/rulesManage",
        name: "rulesManage",
        meta: {
          title: "秩序册管理",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "rulesManage" */ "../views/RulesManage.vue"
          ),
      },
      {
        path: "/userManage",
        name: "userManage",
        meta: {
          title: "用户管理",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "userManage" */ "../views/UserManage.vue"
          ),
      },
      {
        path: "/permissionRequest",
        name: "refereeManage",
        meta: {
          title: "权限申请",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "refereeManage" */ "../views/RefereeManage.vue"
          ),
      },
      {
        path: "/menuManage",
        name: "menuManage",
        meta: {
          title: "菜单管理",
          isAuth: true,
        },
        component: () =>
          import(
            /* webpackChunkName: "menuManage" */ "../views/MenuManage.vue"
          ),
      },
      {
        path: "/icon",
        name: "icon",
        meta: {
          title: "自定义图标",
          isAuth: true,
        },
        component: () =>
          import(/* webpackChunkName: "icon" */ "../views/Icon.vue"),
      },
      {
        path: "/profile",
        name: "profile",
        meta: {
          title: "个人中心",
        },
        component: () =>
          import(/* webpackChunkName: "profile" */ "../views/Profile.vue"),
      },
      {
        path: "/403",
        name: "403",
        meta: {
          title: "没有权限",
        },
        component: () =>
          import(/* webpackChunkName: "403" */ "../views/403.vue"),
      },
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
    },
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/Login.vue"),
  },
  {
    path: "/register",
    name: "Register",
    meta: {
      title: "注册",
    },
    component: () =>
      import(/* webpackChunkName: "register" */ "../views/Register.vue"),
  },

  {
    path: "/404",
    name: "404",
    meta: {
      title: "找不到资源",
    },
    component: () => import(/* webpackChunkName: "404" */ "../views/404.vue"),
  },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

//全局前置路由守卫，每次路由切换前都会调用，检查权限
router.beforeEach((to, from, next) => {
  const hasToken = store.state.token;
  
 
  if (hasToken) {
    // console.log("进入了");
    if (to.path === "/login") {
      next("/home");
    } else {
        next(); // 确保一定要调用 next()
    }
  } else {
    if (to.path === "/login" || to.path === "/register") {
      next();
    } else {
      next("/login");
    }
  }

  // next()
});


//全局后置路由守卫，切换页面标题
router.afterEach((to, from) => {
    document.title =  to.meta.title || '武术赛事编排管理系统';
});


export default router;